using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Search.PageAdapters.Utilities;

namespace Search.PageAdapters
{
    /// <summary>
    /// This feature customizes (unghosts) the 4 core page layouts used by the Enterprise Search Center to use a custom placeholder to host the search controls. This allows master functionality based on the v4.master (e.g. navigation, breadcrumbs) to work in the Search Center This feature will not work with the minimal.master and replaces the use of the PlaceHolder "PlaceHolderTitleBreadcrumb" with a custom PlaceHolder "PlaceHolderSearchControls". Deactivating this feature will uncustomize (reghost) the page layouts.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("72f0e0c7-4fca-4f41-b097-88fa979086bf")]
    public class EnterpriseSearchCompatibilityEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            // The following if/else allows the feature scope to be changed without any significant refactoring required.
            if (properties.Feature.Parent is SPSite)
            {
                SPSite site = properties.Feature.Parent as SPSite;
                if (site != null)
                {
                    web = site.RootWeb;
                }
            }
            else if (properties.Feature.Parent is SPWeb)
            {
                web = properties.Feature.Parent as SPWeb;                
            }

            try
            {
                if (web != null)
                {
                    SearchAdapters.ApplyCustomSearchFix(web, true);
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("There was an error while activating the feature {0}", properties.Feature.ToString()), e);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            // The following if/else allows the feature scope to be changed without any significant refactoring required.
            if (properties.Feature.Parent is SPSite)
            {
                SPSite site = properties.Feature.Parent as SPSite;
                if (site != null)
                {
                    web = site.RootWeb;
                }
            }
            else if (properties.Feature.Parent is SPWeb)
            {
                SPWeb _web = properties.Feature.Parent as SPWeb;
                if (_web != null)
                    web = _web.Site.RootWeb;
            }

            try
            {
                if (web != null)
                {
                    SearchAdapters.ApplyCustomSearchFix(web, false);
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("There was an error while deactivating the feature {0}", properties.Feature.ToString()), e);
            }
        }
    }
}
